package org.nowcoder.leetcode.arrays;

/**
 * Title  : 58. Length of Last Word
 * Source : https://leetcode.cn/problems/length-of-last-word/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-07-10
 */

public class LeetCode_58 {
    public int lengthOfLastWord(String s) {
        //暴力解法： 常规解法
        // String str = s.trim();
        // str = str.replaceAll(" ", "#");
        // String[] strs = str.split("#");
        // return strs[strs.length - 1].length();

        int len = 0;
        for (int i = s.length() - 1; i >= 0; i--) {
            char c = s.charAt(i);
            if (len != 0 && c == ' ') return len;
            if (c != ' ') {
                len++;
            }
        }
        return len;
    }
}
